var _ = require('underscore')
var SocketTool = {}
//获取所有命名空间
SocketTool.SearchNamespaces = (cb) => {
    db.select("socket_namespace",(rs) => {
        cb(rs)
    })
}
//进行token的验证
SocketTool.vToken = (data, cb) => {

    db.where({
        "ut_token" : data.token,
        "ut_is_delete" : 0
    }).select("socket_user_token",(rs) => {

        //不存在token
        if(rs.length == 0){
            return cb(false,404,"token值不正确，请登陆")
        }
        //进行token是否过期的验证
        if(common.nowTime() >= data.ut_create_time + config.publicConfig.TOKENTIME){
            return cb(false,404,"token值已过期了，请重新登陆")
        }
        cb(true)
    })

}
//进行socketUser的存储
SocketTool.vSocketUser = (data, socket, cb) => {
    //根据token查询用户信息
    var userData = {}//临时存放用户基本信息

    db.where({
        "ut_token"     : data.token,
        "ut_is_delete" : 0
    }).select("socket_user_token",(rs)=>{
        var uId = rs[0].ut_uid
        userData.token = data.token
        userData.ID  = uId

        db.where({
            "u_username"     : uId,
            "u_is_delete" : 0
        }).select("socket_user",(res)=>{

            userData.name        = res[0].u_nickname
            userData.portraitUri = res[0].u_header
            //获取appId
            db.where({
                "n_id"     : res[0].u_nid,
            }).select("socket_namespace",(res)=>{
                userData.appId       = res[0].n_appid
            })
            //进行数据的存储与更新


            var key = "U_" + userData.ID

            if(socket){
                socketUser[key] = {
                    "name"           : userData.name,
                    "portraitUri"    : userData.portraitUri,
                    "token"          : userData.token,
                    "appId"          : userData.appId,
                    "userSocketId"   : socket.id
                }

            }else{
                if(socketUser[key]){
                    socketUser[key].name        = userData.name
                    socketUser[key].portraitUri = userData.portraitUri
                }

            }

            cb()
        })
    })
}
//获取socket
SocketTool.vObtainSocketUser = (userId) => {

    var key =  "U_" + userId

    if(!socketUser[key]){
        return false
    }

    var Id = socketUser[key].userSocketId


    var toSocket = _.findWhere(namespaceName.sockets,{id:Id});

    return toSocket
}
//创建房间存储
SocketTool.vCreateRoom = (roomData) => {

    var key =  "R_" + roomData.chatName
    chatRoom[key] = {
        "roomMasterId" : roomData.masterName,
        "roomId"       : roomData.chatName,
        "isOnLine"     : true,
        "member"       : {},
    }
    console.log("创建成功了")
    return chatRoom
}
//销毁房间存储
SocketTool.vDestroyRoom = (roomData) => {

    var key =  "R_" + roomData.id
    delete(chatRoom[key]);
    console.log("销毁成功了")
    return chatRoom
}
//用户加入房间
SocketTool.vJoinRoom = (datas) => {
    var key =  "R_" + datas.chatName
    //查询用户信息socket
    var toSocket = this.socketTool.vObtainSocketUser(datas.userName)
    if(datas.type == "chatRoom"){
        //把用户信息加入到聊天室的全局变量里
        toSocket.join(key);

        var userKey = "RU_" + datas.userName

        chatRoom[key].member[userKey] = {
            userName : datas.userName,
            jionTime : common.nowTime()
        }
    }
    return key
}
//用户退出房间
SocketTool.vSignOutRoom = (datas) => {
    var key =  "R_" + datas.Id
    //查询用户信息socket
    var toSocket = this.socketTool.vObtainSocketUser(datas.u_username)
    if(datas.type == "chatRoom"){
        //把用户信息从聊天室的全局变量里删除
        toSocket.leave(key);
        var userKey = "RU_" + datas.u_username
        delete(chatRoom[key].member[userKey]);
    }
    return key
}

exports.socketTool = SocketTool